package com.zenithmind.mall.controller;

import com.zenithmind.common.annotation.Log;
import com.zenithmind.common.annotation.RequireAuth;
import com.zenithmind.common.constant.OperationTypeEnum;
import com.zenithmind.common.context.UserContext;
import com.zenithmind.common.result.Result;
import com.zenithmind.mall.pojo.dto.CartDTO;
import com.zenithmind.mall.pojo.vo.CartVO;
import com.zenithmind.mall.service.CartService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 购物车控制器
 * 
 * @author ZenithMind Team
 * @since 2025-06-09
 */
@Slf4j
@RestController
@RequestMapping("/api/zenithMind/mall/cart")
@RequiredArgsConstructor
@RequireAuth
@Tag(name = "购物车管理", description = "购物车相关接口")
public class CartController {

    private final CartService cartService;

    @PostMapping
    @Operation(summary = "添加商品到购物车", description = "将商品加入购物车")
    @Log(description = "添加商品到购物车", operationType = OperationTypeEnum.ADD)
    public Result<Boolean> addToCart(@Validated @RequestBody CartDTO cartDTO) {
        String userId = UserContext.requireUserId();
        log.info("用户 {} 添加商品到购物车: {}", userId, cartDTO.getProductId());
        Boolean result = cartService.addToCart(cartDTO, userId);
        return Result.success(result);
    }

    @GetMapping
    @Operation(summary = "查询购物车列表", description = "获取当前用户的购物车商品列表")
    public Result<List<CartVO>> getCartList() {
        String userId = UserContext.requireUserId();
        log.info("查询用户购物车列表: {}", userId);
        List<CartVO> result = cartService.getCartList(userId);
        return Result.success(result);
    }

    @PutMapping("/{id}/quantity")
    @Operation(summary = "更新购物车商品数量", description = "修改购物车中商品的数量")
    @Parameter(name = "id", description = "购物车ID", required = true)
    @Parameter(name = "quantity", description = "新的数量", required = true)
    @Log(description = "更新购物车商品数量", operationType = OperationTypeEnum.UPDATE)
    public Result<Boolean> updateQuantity(@PathVariable String id, @RequestParam Integer quantity) {
        String userId = UserContext.requireUserId();
        log.info("用户 {} 更新购物车商品数量: {} -> {}", userId, id, quantity);
        Boolean result = cartService.updateCartQuantity(id, quantity, userId);
        return Result.success(result);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除购物车商品", description = "从购物车中移除指定商品")
    @Parameter(name = "id", description = "购物车ID", required = true)
    @Log(description = "删除购物车商品", operationType = OperationTypeEnum.DELETE)
    public Result<Boolean> removeFromCart(@PathVariable String id) {
        String userId = UserContext.requireUserId();
        log.info("用户 {} 删除购物车商品: {}", userId, id);
        Boolean result = cartService.removeFromCart(id, userId);
        return Result.success(result);
    }

    @DeleteMapping("/batch")
    @Operation(summary = "批量删除购物车商品", description = "批量删除多个购物车商品")
    @Log(description = "批量删除购物车商品", operationType = OperationTypeEnum.DELETE)
    public Result<Boolean> removeFromCart(@RequestBody List<String> ids) {
        String userId = UserContext.requireUserId();
        log.info("用户 {} 批量删除购物车商品: {}", userId, ids);
        Boolean result = cartService.removeFromCart(ids, userId);
        return Result.success(result);
    }

    @PutMapping("/{id}/select")
    @Operation(summary = "选中/取消选中购物车商品", description = "设置购物车商品的选中状态")
    @Parameter(name = "id", description = "购物车ID", required = true)
    @Parameter(name = "selected", description = "是否选中（1-选中，0-取消选中）", required = true)
    public Result<Boolean> selectCartItem(@PathVariable String id, @RequestParam Integer selected) {
        String userId = UserContext.requireUserId();
        log.info("用户 {} 设置购物车商品选中状态: {} -> {}", userId, id, selected);
        Boolean result = cartService.selectCartItem(id, selected, userId);
        return Result.success(result);
    }

    @PutMapping("/select-all")
    @Operation(summary = "全选/取消全选", description = "设置购物车中所有商品的选中状态")
    @Parameter(name = "selected", description = "是否选中（1-全选，0-取消全选）", required = true)
    public Result<Boolean> selectAllCartItems(@RequestParam Integer selected) {
        String userId = UserContext.requireUserId();
        log.info("用户 {} 设置购物车全选状态: {}", userId, selected);
        Boolean result = cartService.selectAllCartItems(selected, userId);
        return Result.success(result);
    }

    @DeleteMapping("/selected")
    @Operation(summary = "删除选中的商品", description = "删除购物车中所有选中的商品")
    @Log(description = "删除选中的购物车商品", operationType = OperationTypeEnum.DELETE)
    public Result<Boolean> removeSelectedItems() {
        String userId = UserContext.requireUserId();
        log.info("用户 {} 删除选中的购物车商品", userId);
        Boolean result = cartService.removeSelectedItems(userId);
        return Result.success(result);
    }

    @GetMapping("/count")
    @Operation(summary = "获取购物车商品数量", description = "获取购物车中商品的总数量")
    public Result<Integer> getCartCount() {
        String userId = UserContext.requireUserId();
        Integer result = cartService.getCartCount(userId);
        return Result.success(result);
    }

    @DeleteMapping("/clear")
    @Operation(summary = "清空购物车", description = "清空购物车中的所有商品")
    @Log(description = "清空购物车", operationType = OperationTypeEnum.DELETE)
    public Result<Boolean> clearCart() {
        String userId = UserContext.requireUserId();
        log.info("用户 {} 清空购物车", userId);
        Boolean result = cartService.clearCart(userId);
        return Result.success(result);
    }
}
